package org.javaopenaccess.dao.schema;

import java.util.List;

import org.javaopenaccess.dao.AbstractDb4oSupportDao;
import org.javaopenaccess.model.Schema;
import org.javaopenaccess.service.schema.SchemaNotFoundException;

import com.db4o.query.Predicate;

public class Db4oSchemaDao extends AbstractDb4oSupportDao implements SchemaDao{

	@Override
	public void store(Schema schema) throws SchemaSaveException {
		try {
			getSession().store(schema);
		} catch (Exception e) {
			throw new SchemaSaveException("Could not store schema", e);
		}
	}

	@SuppressWarnings("serial")
	@Override
	public Schema getSchema(final String name) throws SchemaNotFoundException {	
		List<Schema> list = getSession().query(new Predicate<Schema>() {
			public boolean match(Schema candidate) {
				return candidate.getName().equals(name);
			}
		});
		if (list.isEmpty())
				throw new SchemaNotFoundException("No schema found");
		return list.get(0);
	}

}
